package toanstt.gameplay;

import toanstt.com.T;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;

import android.text.format.Time;
import android.util.Log;

public class TimeLine 
{
	float MAXTIME;
	float CURRENT_TIME;
	float float_temp;
	long start_system_time;
	long current_system_time;
	float playing_system_time;
	float d_system_time;
	boolean ispause = false;
	float percent;
	int D_DRAW;
	boolean isstage = false;
	
	public TimeLine(int t)
	{
		playing_system_time = 0;
		MAXTIME = t;
		CURRENT_TIME =0;
		D_DRAW = T.GAMESCREEN_MAX_X- T.GAMESCREEN_MIN_X;
		 start_system_time =System.currentTimeMillis();
	}
	public TimeLine(int  stage,boolean isstage2)
	{
		playing_system_time = 0;
		isstage = true;
		MAXTIME = stage;
		CURRENT_TIME =0;
		D_DRAW = T.GAMESCREEN_MAX_X- T.GAMESCREEN_MIN_X;
		 start_system_time =System.currentTimeMillis();
	}
	public void AddTime(int s)
	{
		float_temp = s*1.0f/500;
		if(CURRENT_TIME-float_temp <0)
			CURRENT_TIME = 0;
		else
			CURRENT_TIME-=float_temp;
	}
	public float GetPercent()
	{
		return CURRENT_TIME/MAXTIME;
	}
	public void Tick()
	{
		
		if(isstage==true)
		{
			if(CURRENT_TIME>=MAXTIME)
				T.notice.DoNotice(1,0);
			TickStage();
			return;
		}
		if(percent>=1)
			T.notice.DoNotice(0,0);
		
		if(playing_system_time>120)
		{
			playing_system_time = 0;
			T.GAME_NUM_DIAMOND++;
			T.dialog.DoDialog("Difficult Up");
			if(T.GAME_NUM_DIAMOND>T.GAME_NUM_DIAMOND_MAX)
				T.GAME_NUM_DIAMOND = T.GAME_NUM_DIAMOND_MAX;
		}
		long h = System.currentTimeMillis();
		current_system_time = System.currentTimeMillis();
		d_system_time = (current_system_time-start_system_time);
		if(ispause==false)
		{
			CURRENT_TIME+=d_system_time/1000.0f;
			playing_system_time+= d_system_time/1000.0f;
			//Log.d("aa", d_system_time+"");
		}
		start_system_time =current_system_time; 
	}
	
	public void TickStage()
	{
		playing_system_time += T.PLAY_SCORE -CURRENT_TIME;
//		if(playing_system_time>7500)
//		{
//			T.GAME_NUM_DIAMOND++;
//			T.dialog.DoDialog("Difficult Up");
//			if(T.GAME_NUM_DIAMOND>T.GAME_NUM_DIAMOND_MAX)
//				T.GAME_NUM_DIAMOND = T.GAME_NUM_DIAMOND_MAX;
//			playing_system_time =0;
//		}
		CURRENT_TIME = T.PLAY_SCORE;
		
	}
	public void Draw(Canvas canvas)
	{
		//CURRENT_TIME= 20;
		percent = CURRENT_TIME/MAXTIME;
		T.paint.setColor(Color.BLUE);
		if(percent>=1)
		{
			percent = 1;
			canvas.drawRect(T.GAMESCREEN_MIN_X, T.GAMESCREEN_MAX_Y, T.GAMESCREEN_MIN_X + percent*D_DRAW, T.GAMESCREEN_MAX_Y+T.TIMELINE_HEIGHT, T.paint);
		}
		else
		{
			canvas.drawRect(T.GAMESCREEN_MIN_X, T.GAMESCREEN_MAX_Y, T.GAMESCREEN_MIN_X + percent*D_DRAW, T.GAMESCREEN_MAX_Y+T.TIMELINE_HEIGHT, T.paint);
			T.mat = new Matrix();
			T.mat.postScale(T.TIMELINE_HEIGHT*1.0f/20,T.TIMELINE_HEIGHT*1.0f/20);
			T.mat.postTranslate(T.GAMESCREEN_MIN_X + percent*D_DRAW-T.TIMELINE_HEIGHT/2, T.GAMESCREEN_MAX_Y);
			canvas.drawBitmap(T.bitmap_timeline_anchor, T.mat, null);
		}
	}
}
